import { evalEl, getThisObjCommonSetVal } from './auth'
import { getCheckRecordKeyStr, getGlobalParam } from './hyrecord'

export function initDealApi(thisObj, val) {
  thisObj.dealApi = Object.assign({}, thisObj.dealApi, val.urlList.url)
  thisObj.dealParam = Object.assign({}, thisObj.dealParam, val.urlList.param)
}

export function getRemoteUrlDataSetKey(thisObj, urlKey) {
  return thisObj.dealParam[urlKey]
}

export function getUrlParamList(urlParamKey) {
  const tmpObj = getUrlParamCommonObj(urlParamKey, 2)
  return tmpObj
}

export function getUrlParamMap(urlParamKey) {
  const tmpObj = getUrlParamCommonObj(urlParamKey, 4)
  return tmpObj
}

export function getUrlParamCommonObj(urlParamKey, keyType) {
  const tmpObj = {}
  if (undefined !== urlParamKey && urlParamKey.length > 0) {
    for (const i in urlParamKey) {
      const paramData = urlParamKey[i]
      if (paramData.objCompType === keyType) {
        const parentKey = paramData.parentKey
        let parentObj = tmpObj[parentKey]
        if (parentObj === undefined) {
          parentObj = {}
          parentObj.keyList = []
          tmpObj[parentKey] = parentObj
        }
        if (paramData.valObj === 4) {
          parentObj.valObj = paramData.valObj
          parentObj.parentVal = paramData.parentVal
        }
        let tmpArr = parentObj.keyList
        if (tmpArr !== undefined) {
          tmpArr.push(paramData)
          parentObj.keyList = tmpArr
        } else {
          tmpArr = []
          tmpArr.push(paramData)
          parentObj.keyList = tmpArr
        }
      }
    }
  }
  return tmpObj
}

/**
 * 默认参数列表
 * @param urlParamKey
 * @returns {*[]}
 */
export function getUrlDefaultParam(urlParamKey) {
  const paramList = []
  if (undefined !== urlParamKey && urlParamKey.length > 0) {
    for (const i in urlParamKey) {
      const paramData = urlParamKey[i]
      if (paramData.objCompType === undefined || paramData.objCompType === 1) {
        paramList.push(paramData)
      }
    }
  }
  return paramList
}

export function getRemoteUrlParam(thisObj, urlKey, globalData) {
  const urlData = getRemoteUrlPathData(thisObj, urlKey)
  if (urlData === null) {
    return
  }
  let urlParamKey = {}
  if (thisObj.dealParam[urlKey]) {
    urlParamKey = Object.assign([], thisObj.dealParam[urlKey].S_DY_PARAM)
  }
  const urlParam = getReqParamData(thisObj, globalData, urlParamKey)
  const remoteUrlParam = Object.assign({}, urlParam, urlData)
  const clickBtn = Object.assign({}, thisObj.uiPageUrl)

  if (globalData.clickItem !== undefined && globalData.clickItem !== null) {
    clickBtn.S_UI_BTN_IDX_2025 = globalData.clickItem.id
    clickBtn.S_UI_NAME_IDX_2025 = globalData.clickItem.name
    clickBtn.S_UI_FN_TYPE_2025 = globalData.clickItem.fnType
  }
  remoteUrlParam.S_UI_CLICK_BTN_2025 = clickBtn
  return remoteUrlParam
}

export function getFnInputParam(thisObj, globalData, refSetOpenVal) {
  if (refSetOpenVal !== undefined) {
    return getReqParamData(thisObj, globalData, refSetOpenVal)
  } else {
    return globalData.openParam
  }
}

export function getReqParamData(thisObj, globalData, urlParamKey) {
  const reqParamData = {}

  let selectedRows = []
  if (globalData.tableSelectedRow !== undefined) {
    selectedRows = globalData.tableSelectedRow
  }
  const urlParamListObj = getUrlParamList(urlParamKey)
  for (const urlParamObjIdx in urlParamListObj) {
    const urlParamObj = urlParamListObj[urlParamObjIdx]
    const valObj = urlParamObj.valObj
    if (valObj === 4) {
      // 本地列表
      selectedRows = globalData[urlParamObj.parentVal]
    }

    const urlParamList = urlParamObj.keyList
    const newRowsList = []
    const selectedRowsLength = selectedRows.length
    for (let i = 0; i < selectedRowsLength; i++) {
      const tmpGlobalData = Object.assign({}, globalData)
      tmpGlobalData.clickRow = selectedRows[i]
      tmpGlobalData.globalParam = getGlobalParam(thisObj)
      const listRow = getNewCommonUrlParam(thisObj, tmpGlobalData, selectedRows[i], urlParamList)
      newRowsList.push(listRow)
    }
    reqParamData[urlParamObjIdx] = newRowsList
  }
  const checkRecordStr = getCheckRecordKeyStr(selectedRows)
  const urlParamMapObj = getUrlParamMap(urlParamKey)
  for (const urlParamObjIdx in urlParamMapObj) {
    const urlParamObj = urlParamMapObj[urlParamObjIdx]
    const urlParamList = urlParamObj.keyList
    const mergeRow = getNewCommonUrlParam(thisObj, globalData, checkRecordStr, urlParamList)
    reqParamData[urlParamObjIdx] = mergeRow
  }
  const urlDefaultParamList = getUrlDefaultParam(urlParamKey)
  const urlParam = getNewCommonUrlParam(thisObj, globalData, checkRecordStr, urlDefaultParamList)
  const remoteUrlParam = Object.assign({}, urlParam, reqParamData)
  return remoteUrlParam
}

/**
 * 获取请求路径
 * @param thisObj 当前对象
 * @param urlKey 地址变量
 */
export function getRemoteUrlPathData(thisObj, urlKey) {
  const urlData = Object.assign({}, thisObj.dealApi[urlKey])
  return getRemoteUrlPathInitData(thisObj, urlKey, urlData)
}

/**
 * 获取提交参数
 * @param thisObj 当前对象
 * @param globalData
 * @param rowData  行记录
 * @param urlParamKey
 * @returns {{}} 返回参数变量对应数据
 */
export function getNewCommonUrlParam(thisObj, globalData, rowData, urlParamKey) {
  const postParam = {}
  if (undefined !== urlParamKey && urlParamKey.length > 0) {
    for (const i in urlParamKey) {
      const tempData = urlParamKey[i]
      const objEl = tempData.objEl
      if (objEl !== undefined) {
        if (!evalEl(objEl, globalData.globalParam)) {
          continue
        }
      }
      let val
      if (tempData.valObj === 2) { // 下拉枚举
        val = getObjEnumVal(thisObj, globalData, tempData)
      } else if (tempData.valObj === 4) { // 选择列表
        val = getThisObjCommonSetVal(thisObj, globalData, rowData, tempData)
      } else {
        val = getThisObjCommonSetVal(thisObj, globalData, rowData, tempData)
      }
      if (val === undefined) {
        val = ''
      }
      postParam[tempData.key] = val
    }
  }
  return postParam
}

/**
 * 获取列表对象数据
 * @param thisObj
 * @param record
 * @param urlParamData
 * @returns {*[]}
 */
export function getReturnSetFormData(thisObj, record, urlParamData) {
  const okList = []
  const returnSetFormData = thisObj.initReturnSetFormData[urlParamData.objComp]
  if (returnSetFormData !== undefined) {
    for (const eIdx in returnSetFormData) {
      const setFormData = returnSetFormData[eIdx]
      const val = setFormData[urlParamData.val]
      okList.push(val)
    }
  }
  return okList
}
/**
 * 获取列表对象数据
 * @param thisObj
 * @param record
 * @param urlParamData
 * @param urlKey 数据集变量
 * @returns {*[]}
 */
export function getReturnSetListData(thisObj, record, urlParamData, urlKey) {
  const okList = []
  const returnSetListData = thisObj.initReturnSetListData[urlKey]
  if (returnSetListData !== undefined) {
    for (const eIdx in returnSetListData) {
      const setListData = returnSetListData[eIdx]
      const val = setListData[urlParamData.val]
      okList.push(val)
    }
  }
  return okList
}

/**
 * 获取表单开窗对象数据 (过期)
 * @param thisObj
 * @param record
 * @param urlParamData
 * @returns {*[]}
 */
export function getObjReturnSetFormData(thisObj, record, urlParamData) {
  const okList = []
  const returnSetFormData = thisObj.initReturnSetFormData[urlParamData.objComp]
  if (returnSetFormData !== undefined) {
    for (const eIdx in returnSetFormData) {
      const setFormData = returnSetFormData[eIdx]
      const val = setFormData[urlParamData.val]
      okList.push(val)
    }
  }
  return okList
}

/**
 * 获取枚举数据
 * @param thisObj
 * @param record
 * @param urlParamData
 * @returns {*[]|*}
 */
export function getObjEnumVal(thisObj, globalData, urlParamData) {
  let enumKey = urlParamData.objComp
  if (enumKey === undefined) {
    enumKey = urlParamData.compTagModel
  }
  const okEnumList = []
  const filterObjData = globalData.formModel[enumKey]
  const enumData = globalData.activeEnumData[enumKey]
  if (filterObjData !== undefined && filterObjData instanceof Array) {
    const len = filterObjData.length
    for (let i = 0; i < len; i++) {
      const iVal = filterObjData[i]
      for (const eIdx in enumData) {
        const enumObj = enumData[eIdx]
        if (iVal === enumObj.value) {
          okEnumList.push(getEnumVal(enumObj, urlParamData))
        }
      }
    }
    return okEnumList
  } else {
    for (const eIdx in enumData) {
      const enumObj = enumData[eIdx]
      if (filterObjData === enumObj.value) {
        return getEnumVal(enumObj, urlParamData)
      }
    }
  }
  return
}

export function getEnumVal(filterEnumData, urlParamData) {
  if (filterEnumData !== null) {
    if (urlParamData.parentVal !== undefined) {
      const paramVal = filterEnumData[urlParamData.parentVal]
      if (undefined !== paramVal) {
        return paramVal[urlParamData.val]
      }
    } else {
      return filterEnumData[urlParamData.val]
    }
  }
  return
}

/**
 * 获取请求路径
 * @param thisObj 当前对象
 * @param urlKey 地址变量
 * @param urlData 地址
 */
export function getRemoteUrlPathInitData(thisObj, urlKey, urlData) {
  if (urlData.url === '/run/ctr/submitJson') {
    if (urlData.S_PAGE_ID === null || urlData.S_PAGE_ID === '') {
      thisObj.$message.warning('请求数据路径未配置.')
      return null
    }
  }
  return urlData
}
export function getCommonRemoteUrlInitVerify(thisObj, urlKey, postParam, urlParamKey) {
  if (undefined !== urlParamKey && urlParamKey.length > 0) {
    for (const i in urlParamKey) {
      const tempData = urlParamKey[i]
      if (tempData.keyNull === 1) {
        if (postParam[tempData.key] === undefined || postParam[tempData.key] === '') {
          // 验证不通过
          const tipMsg = tempData.name + '[' + tempData.key + ']值为空.'
          thisObj.$message.warning(tipMsg)
          return 1
        }
      }
    }
  }
  return 0
}

export function getCommonRemoteUrlVerify(thisObj, urlKey, postParam) {
  let urlParamKey = {}
  if (thisObj.dealParam[urlKey]) {
    urlParamKey = Object.assign([], thisObj.dealParam[urlKey].S_DY_PARAM)
  }
  return getCommonRemoteUrlInitVerify(thisObj, urlKey, postParam, urlParamKey)
}

export function getUrlKey(operator = null, btnItem) {
  let urlKey = null
  if (operator === null) {
    if (undefined !== btnItem && undefined !== btnItem.urlKey) {
      urlKey = btnItem.urlKey
      if (btnItem.urlKey === '') {
        urlKey = 'page'
      }
    }
  } else {
    urlKey = operator.s_REF_URL_KEY
  }
  return urlKey
}

export function getTableSelectRow(thisObj) {
  const tmpSelectedRow = thisObj.tableSelectedRow
  if (tmpSelectedRow.length === undefined) {
    return tmpSelectedRow
  } else {
    if (tmpSelectedRow.length === 0) {
      return null
    } else {
      return tmpSelectedRow[0]
    }
  }
}
export function handleVerifyErrorTips(thisObj, result) {
  const data = result.data
  const h = thisObj.$createElement
  const demo = []
  for (const i in data) {
    const item = data[i]
    demo.push(h('p', item.verifyResult))
  }
  thisObj.$error({
    title: result.resultMsg,
    content: h('div', {}, demo)
  })
}
